package cn.ococci.dm.Base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import cn.ococci.dm.Main.Activity.MainActivity;
import cn.ococci.dm.Main.Const;
import cn.ococci.dm.Main.View.TitleBar;
import cn.ococci_dm.R;


/**
 * Created by cyan on 15/5/24.
 * 所有的fragment都继承于BaseFragment
 */
public abstract class BaseFragment extends android.support.v4.app.Fragment implements BaseController{

    private View contentView;
    private TitleBar titleBar;

    public BaseFragment lastFragment;

    private boolean backFlag = false;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if(contentView == null){
            View view  = inflater.inflate(getResId(),container,false);
            if(showTitleBar()){
                FrameLayout fl = new FrameLayout(inflater.getContext());
                titleBar = new TitleBar(inflater.getContext());
                int height = (int)getResources().getDimension(R.dimen.title_bar_height);
                FrameLayout.LayoutParams lp =new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,height);
                fl.addView(titleBar,lp);
                lp =new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                lp.topMargin = height;
                fl.addView(view,lp);
                view = fl;
            }
            contentView = view;
        }

        if(backFlag){
            backFlag = false;
            initBackData();
        }else{
            initView(contentView);
            initData();
        }
        initAdapter();
        initListener();
        initReceiver();
        initTitleBar();
        initViewColor(BaseApp.COLOR);

        return contentView;
    }


    protected abstract int getResId();

    protected abstract void initView(View parent);

    protected abstract void initData();

    protected void initAdapter(){}

    protected void initListener(){}

    protected void initReceiver(){}

    protected void initViewColor(int color){}

    protected void destroyReceiver(){}

    public void initTitleBar(){
        if(titleBar != null){
            titleBar.setAdapter(this);
        }
    }
    public boolean showTitleBar() {
        return true;
    }


    public FragmentController getFragmentContainer(){
        return (FragmentController) getActivity();
    }

    public void initBackData(){
        this.initData();
    }

    public void setLastFragment(BaseFragment lastFragment) {
        this.lastFragment = lastFragment;
    }


    public MainActivity getMainActivity() {
        return (MainActivity) this.getActivity();
    }


    public void turnTo(BaseFragment fragment){
        fragment.setLastFragment(this);
        this.turnTo(fragment, Const.ANIM_NEXT);
    }

    public void turnTo(BaseFragment fragment,int animType){
        if(getFragmentContainer() != null){
            getFragmentContainer().setFragment(fragment,animType);
        }
    }


    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        getBaseActivity().hideInput();
    }


    @Override
    public void onDestroy() {
        destroyReceiver();
        super.onDestroy();
    }

    @Override
    public boolean goBack() {
        if (lastFragment != null) {
            lastFragment.backFlag = true;
            getFragmentContainer().setFragment(lastFragment, Const.ANIM_BACK);
            return true;
        }

        return false;
    }



    @Override
    public BaseActivity getBaseActivity() {
        return (BaseActivity) getActivity();
    }

    @Override
    public void upDateTitleBar() {
        this.getBaseActivity().upDateTitleBar();
    }


    @Override
    public void onBackClick() {
        this.goBack();
    }



    @Override
    public String getBackCaption() {
        return null;
    }

    @Override
    public String getCaption() {
        return null;
    }



    @Override
    public int getCaptionSize() {
        return 0;
    }

    @Override
    public void onRightTextClick() {}

    @Override
    public String getRightText() {
        return null;
    }

    @Override
    public int getRightTextSize() {
        return 0;
    }

    @Override
    public void onRightImageClick() {

    }

    @Override
    public int getRightImageResId() {
        return 0;
    }

    @Override
    public String[] getRadioTitles() {
        return null;
    }

    @Override
    public int getRadioDefaultIndex() {
        return 0;
    }

    @Override
    public void onRadioClicked(int index) {
    }

    @Override
    public void onCaptionClicked() {

    }
}
